package com.hzdl.apple.controller.cartCotroll;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzdl.apple.bean.Good;
import com.hzdl.apple.bean.User;
import com.hzdl.apple.bean.cart.CartItem;
import com.hzdl.apple.service.GoodsService;
import com.hzdl.apple.service.cartService.CartService;
import com.hzdl.apple.utils.DruidUtils;
import com.hzdl.apple.utils.HttpUtils;

/**
 * 购物车
 */
@WebServlet("/user/cart/*")
public class CartController extends HttpServlet {

	private GoodsService goodsServece;
	private CartService cartService;
	private Connection conn;
	private Good g;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		goodsServece = new GoodsService();
		cartService = new CartService();
		getServletContext().setAttribute("path", getServletContext().getContextPath());
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String baseUri = req.getRequestURI();
		Connection conn;
		try {
			conn = DruidUtils.dataSource.getConnection();
			
			String color="";
			
			// int goodsID=1;//测试
//			int goodsID = Integer.parseInt("1");
			
			// 获取当前用户
			User user = (User) req.getSession().getAttribute("user");
			double inch = 5.5;//已经弃用的属性
			// double inch =(double) req.getSession().getAttribute("inch");

			if (baseUri.contains("/addCart")) {// 跳转购物车页面
				// 获取到要添加商品的id
				int GID=Integer.parseInt(req.getParameter("GoodsID"));
				String chooseColor=req.getParameter("chooseColor");
				// 通过id查找到该商品
				g = goodsServece.getGoodById(GID, conn);
				switch (chooseColor) {
				case "black":
					color="黑色";
					break;
				case "white":
					color="白色";
					break;
				case "blue":
					color="蓝色";
					break;
				case "gold":
					color="金色";
					break;
				case "silvery":
					color="银色";
					break;
				case "gray":
					color="深空灰色";
					break;
				case "rosegold":
					color="玫瑰金色";
					break;
				}
				// 3.把商品转换成CartItem
				CartItem cartItem = new CartItem(g.getGoodsID(), g.getGoodsName(), g.getPrice(), user.getUID(),
						g.getGoodsImg(), color, 5.5, g.getIPadConn(), g.getMacDisc());
				// g.getGoodsImg(), g.getGoodsColor(), inch, g.getIPadConn(),
				// g.getMacDisc());jx
				
				// 4. 把caritem添加到数据库中

				cartService.addCart(cartItem);
				getCarts(req, resp, user);
			} else if (baseUri.contains("/showCarts")) {// 查看购物车所有
				getCarts(req, resp, user);
			} else if (baseUri.contains("/deleteCart")) {// 删除购物车item信息
				int cid = Integer.parseInt(req.getParameter("CID"));
				String errMsg = cartService.deleteCart(cid);
				req.getSession().setAttribute("errMsg", errMsg);
				getCarts(req, resp, user);
			} else if (baseUri.contains("/update")) {// 修改购物车信息
				req.getSession().setAttribute("good", g);
				int id = Integer.parseInt(req.getParameter("id"));
				int nums = Integer.parseInt(req.getParameter("nums"));
				int curNums = Integer.parseInt(req.getParameter("curNums"));
				if ("1".equals(curNums) && "0".equals(nums)) {// 删除整个item
					cartService.deleteCart(id);
				} else {
					cartService.updateNums(nums, id);
				}

				getCarts(req, resp, user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void getCarts(HttpServletRequest req, HttpServletResponse resp, User user)
			throws ServletException, IOException {
		double AllPrice = 0;
		List<CartItem> cartItems = cartService.getAllCarts(user.getUID());
		for (CartItem c : cartItems) {
			AllPrice += c.getAllPrice();
		}
		req.getSession().setAttribute("cart", cartItems);
		req.getSession().setAttribute("AllPrice", AllPrice);
		HttpUtils.forward("cos/cart.jsp", req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
